/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.scene.web;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.enums.JobStatus;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.modules.scene.entity.SceneCtfEnvironmentInstance;
import com.lczyfz.istep.modules.scene.service.SceneCtfEnvironmentInstanceService;
import com.lczyfz.istep.modules.sys.entity.ScheduleJob;
import com.lczyfz.istep.modules.sys.service.ScheduleJobService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * CTF实验环境实例Controller
 * @author dh
 * @version 2017-05-15
 */
@Controller
@RequestMapping(value = "${adminPath}/scene/sceneCtfEnvironmentInstance")
public class SceneCtfEnvironmentInstanceController extends BaseController {

	@Autowired
	private SceneCtfEnvironmentInstanceService sceneCtfEnvironmentInstanceService;
	@Autowired
	private ScheduleJobService scheduleJobService;
	@ModelAttribute
	public SceneCtfEnvironmentInstance get(@RequestParam(required=false) String id) {
		SceneCtfEnvironmentInstance entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sceneCtfEnvironmentInstanceService.get(id);
		}
		if (entity == null){
			entity = new SceneCtfEnvironmentInstance();
		}
		return entity;
	}
	
	@RequiresPermissions("scene:sceneCtfEnvironmentInstance:view")
	@RequestMapping(value = {"list", ""})
	public String list(SceneCtfEnvironmentInstance sceneCtfEnvironmentInstance, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SceneCtfEnvironmentInstance> page = sceneCtfEnvironmentInstanceService.findPage(new Page<SceneCtfEnvironmentInstance>(request, response), sceneCtfEnvironmentInstance); 
		model.addAttribute("page", page);
		return "modules/scene/sceneCtfEnvironmentInstanceList";
	}

	@RequiresPermissions("scene:sceneCtfEnvironmentInstance:view")
	@RequestMapping(value = "form")
	public String form(SceneCtfEnvironmentInstance sceneCtfEnvironmentInstance, Model model) {
		model.addAttribute("sceneCtfEnvironmentInstance", sceneCtfEnvironmentInstance);
		return "modules/scene/sceneCtfEnvironmentInstanceForm";
	}

	@RequiresPermissions("scene:sceneCtfEnvironmentInstance:edit")
	@RequestMapping(value = "save")
	public String save(SceneCtfEnvironmentInstance sceneCtfEnvironmentInstance, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sceneCtfEnvironmentInstance)){
			return form(sceneCtfEnvironmentInstance, model);
		}
		sceneCtfEnvironmentInstanceService.save(sceneCtfEnvironmentInstance);
		addMessage(redirectAttributes, "保存CTF实验环境实例成功");
		return "redirect:"+Global.getAdminPath()+"/scene/sceneCtfEnvironmentInstance/?repage";
	}
	
	@RequiresPermissions("scene:sceneCtfEnvironmentInstance:edit")
	@RequestMapping(value = "delete")
	public String delete(SceneCtfEnvironmentInstance sceneCtfEnvironmentInstance, RedirectAttributes redirectAttributes) throws Exception{

		try {
			sceneCtfEnvironmentInstanceService.deleteInstance(sceneCtfEnvironmentInstance);

		}catch (Exception e){
			addMessage(redirectAttributes, "删除CTF实验环境实例失败");
			return "redirect:"+Global.getAdminPath()+"/scene/sceneCtfEnvironmentInstance/?repage";
		}
		ScheduleJob scheduleJobs= scheduleJobService.findByJobName("env_instance",sceneCtfEnvironmentInstance.getId());
		if(scheduleJobs!=null&&!scheduleJobs.equals(""))
		{
			scheduleJobs.setStatus(JobStatus.Stopped.getValue());
			scheduleJobs.setDelFlag("1");
			scheduleJobService.save(scheduleJobs);
			scheduleJobService.deleteJob(scheduleJobs);
		}
		addMessage(redirectAttributes, "删除CTF实验环境实例成功");
		return "redirect:"+Global.getAdminPath()+"/scene/sceneCtfEnvironmentInstance/?repage";
	}

}